﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace BierdopjeTV.Import
{
    class FileScanner
    {

        private List<string> ListFiles(string Path)
        {
            List<string> _LocalFiles = new List<string>();
            
            string[] Files = Directory.GetFiles(Path, "*.*", SearchOption.AllDirectories);
            for (int i = 0; i < Files.Length; i++)
            {
                if (IsVideoFile(Files[i]))
                {
                    _LocalFiles.Add(Files[i]);
                }

            }
            return _LocalFiles;
        }



        public List<Episodes> getLocalShows(string Path)
        {
            //collection to hold all local files                
            List<string> _LocalFileList = ListFiles(Path);
            
            List<Episodes> _LocalEpisodes = new List<Episodes>();


            for (int file = 0; file < _LocalFileList.Count; file++)
            {
                _LocalEpisodes.Add(parseEpisodes(_LocalFileList[file])); //add object to collection.
            }

            //Debug.Print(_LocalEpisodes[0].Path + " = teruggegeven");
            return _LocalEpisodes;

        }

        private Episodes parseEpisodes(string FullFileName)
        {
            Episodes _Episode;

            string Pattern = @"\\(?<SeriesName>\w.+)(S)(?<Season>\d{2})(E)(?<Episode>\d{2}).+(?<Source>HDTV|PDTV|DVDRIP|DSR|SDTV).(?<Codec>x264|XviD|DivX)?-?(?<Group>\w+)";
            
           
             Regex r = new Regex(Pattern, RegexOptions.IgnoreCase);

            _Episode = new Episodes();
            _Episode.Path = FullFileName;
            try
            {


                _Episode.ShowName = getRealShowName(r.Match(FullFileName).Result("${SeriesName}"));
                //If showname is filled
                if (_Episode.ShowName.Length > 0)
                {
                    _Episode.ShowID = API.BierdopjeShows.getShowID(_Episode.ShowName);
                }
                //_Episode.ShowName = getRealShowName(r.Match(FullFileName).Result("${SeriesName}"));
                _Episode.Season = Convert.ToByte(r.Match(FullFileName).Result("${Season}"));
                _Episode.Episode = Convert.ToByte(r.Match(FullFileName).Result("${Episode}"));
                _Episode.Codec = r.Match(FullFileName).Result("${Codec}");
                _Episode.Source = r.Match(FullFileName).Result("${Source}");
                _Episode.Group = r.Match(FullFileName).Result("${Group}");


                if (_Episode.ShowID > 0)
                {
                    Import.Episodes.addEpisode(_Episode);
                }
            }
            catch 
            {
                string Pattern2 = @"\\(?<SeriesName>\w.+)(?<Season>\d{2})x(?<Episode>\d{2}).+(?<Source>HDTV|PDTV|DVDRIP|DSR|SDTV).(?<Codec>x264|XviD|DivX)?-?(?<Group>\w+)";

                
                Regex s = new Regex(Pattern2, RegexOptions.IgnoreCase);

                _Episode = new Episodes();
                _Episode.Path = FullFileName;
                try
                {


                    _Episode.ShowName = getRealShowName(s.Match(FullFileName).Result("${SeriesName}"));
                    //If showname is filled
                    if (_Episode.ShowName.Length > 0)
                    {
                        //_Episode.ShowID = BierdopjeTV.API.BierdopjeShows.getShowID(_Episode.ShowName);
                    }
                    //_Episode.ShowName = getRealShowName(r.Match(FullFileName).Result("${SeriesName}"));
                    _Episode.Season = Convert.ToByte(s.Match(FullFileName).Result("${Season}"));
                    _Episode.Episode = Convert.ToByte(s.Match(FullFileName).Result("${Episode}"));
                    _Episode.Codec = s.Match(FullFileName).Result("${Codec}");
                    _Episode.Source = s.Match(FullFileName).Result("${Source}");
                    _Episode.Group = s.Match(FullFileName).Result("${Group}");


                    if (_Episode.ShowID > 0)
                    {
                        //BierdopjeTV.Import.Episodes.addEpisode(_Episode);
                    }
                }
                catch { }


                return _Episode;
            }
            return _Episode;

        }

        /// <summary>
        /// makes 'Show Name\Show.name.' -> 'Show name'
        /// </summary>
        /// <param name="source">source string</param>
        /// <returns></returns>
        private static string getRealShowName(string source)
        {
            int lastBackSlash = source.LastIndexOf(@"\") + 1;
            int length = source.Length - lastBackSlash;

            return source.Substring(lastBackSlash, length).Replace(".", " ").Trim();
        } 

        private bool IsVideoFile(string file)
        {
            if (file.EndsWith(".MKV", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            else if (file.EndsWith(".WMV", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            else if (file.EndsWith(".AVI", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


























    }
}
